<?php

// start session
session_start();

// connect to database
$conn_str = 'host=dbsrv1.cdf.toronto.edu dbname=csc309g23 user=csc309g23 password=ipheid5h';
$conn = pg_connect($conn_str);

// determine the function
$query = 'INSERT INTO games (sport, address, city, country, start, endtime, skill) VALUES (\'' . $_POST['sport'] . '\', \'' . $_POST['address'] . '\', \'' . $_POST['city'] . '\', \'' . $_POST['country'] .'\', \'' . $_POST['start'] . '\', \'' . $_POST['end'] . '\', \'' . $_POST['skill'] . '\')';

// query database
if ($result = pg_query($conn, $query)) {
    $query = 'SELECT LASTVAL()';
    $result = pg_query($conn, $query);
    $row = pg_fetch_row($result);
    $query = 'INSERT INTO rosters (username,gameid,organizer,status,team) VALUES (\'' . $_SESSION['username'] . '\',' . $row[0] . ',\'t\',3,0)';
    $result = pg_query($conn, $query);
    header('Location: gameDetails.php?gameid=' . $row[0]);
} else {
    // go back to page from which user logged in
    header('Location: ' . $_SERVER['HTTP_REFERER']);
}
?>
